package com.ruoyi.gwc.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnowFlakeIDGenerator;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gwc.domain.TCostRecord;
import com.ruoyi.gwc.mapper.TCostRecordMapper;
import com.ruoyi.gwc.service.ITCostRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gwc.mapper.TEtcManagementMapper;
import com.ruoyi.gwc.domain.TEtcManagement;
import com.ruoyi.gwc.service.ITEtcManagementService;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * ETC管理Service业务层处理
 * 
 * @author wp
 * @date 2024-12-13
 */
@Service
public class TEtcManagementServiceImpl extends ServiceImpl<TEtcManagementMapper, TEtcManagement> implements ITEtcManagementService
{
    @Autowired
    private TEtcManagementMapper tEtcManagementMapper;
    @Autowired
    private TCostRecordMapper tCostRecordMapper;

    /**
     * 查询ETC管理
     * 
     * @param ectId ETC管理主键
     * @return ETC管理
     */
    @Override
    public TEtcManagement selectTEtcManagementByEctId(Long ectId)
    {
        return tEtcManagementMapper.selectTEtcManagementByEctId(ectId);
    }

    /**
     * 查询ETC管理列表
     * 
     * @param tEtcManagement ETC管理
     * @return ETC管理
     */
    @Override
    public List<TEtcManagement> selectTEtcManagementList(TEtcManagement tEtcManagement)
    {
        return tEtcManagementMapper.selectTEtcManagementList(tEtcManagement);
    }

    /**
     * 新增ETC管理
     * 
     * @param tEtcManagement ETC管理
     * @return 结果
     */
    @Override
    public int insertTEtcManagement(TEtcManagement tEtcManagement)
    {
        Long ectId = SnowFlakeIDGenerator.getInstance().nextId();
        //新增ect购买记录同步增加一条费用记录
        TCostRecord tCostRecord = new TCostRecord();
        tCostRecord.setRecordId(SnowFlakeIDGenerator.getInstance().nextId());
        tCostRecord.setCompanyId(tEtcManagement.getCompanyId());
        tCostRecord.setEctId(ectId);
        tCostRecord.setVehicleId(tEtcManagement.getVehicleId());
        tCostRecord.setCostType(0L);//0收入1支出
        tCostRecord.setCostName(7L);
        tCostRecord.setBuyTime(tEtcManagement.getBuyTime());
        tCostRecord.setRemarks("etc购买记录");
        tCostRecord.setAmount(tEtcManagement.getAmount());
        tCostRecord.setCreateTime(DateUtils.getNowDate());
        tCostRecord.setCreateBy(getUsername());
        tCostRecordMapper.insertTCostRecord(tCostRecord);

        tEtcManagement.setCreateTime(DateUtils.getNowDate());
        tEtcManagement.setCreateBy(getUsername());
        tEtcManagement.setEctId(ectId);
        return tEtcManagementMapper.insertTEtcManagement(tEtcManagement);
    }

    /**
     * 修改ETC管理
     * 
     * @param tEtcManagement ETC管理
     * @return 结果
     */
    @Override
    public int updateTEtcManagement(TEtcManagement tEtcManagement)
    {
        tEtcManagement.setUpdateTime(DateUtils.getNowDate());
        tEtcManagement.setUpdateBy(getUsername());
        return tEtcManagementMapper.updateTEtcManagement(tEtcManagement);
    }

    /**
     * 批量删除ETC管理
     * 
     * @param ectIds 需要删除的ETC管理主键
     * @return 结果
     */
    @Override
    public int deleteTEtcManagementByEctIds(Long[] ectIds)
    {
        return tEtcManagementMapper.deleteTEtcManagementByEctIds(ectIds);
    }

    /**
     * 删除ETC管理信息
     * 
     * @param ectId ETC管理主键
     * @return 结果
     */
    @Override
    public int deleteTEtcManagementByEctId(Long ectId)
    {
        return tEtcManagementMapper.deleteTEtcManagementByEctId(ectId);
    }
}
